/*
    Description:    FIUMAPS.HTMLPopUp allows the appliation to spawn descriptive html information on locations.
    Author:         Carlos Krefft (krefftc@gmail.com)

*/

FIUMAPS.HTMLPopUp = { Tabs:[], Center:null, ServicesGridDiv:null,
    getMarkerHTMLWindows: function (attributes) {
        var html = "<div id='HTMLWindow'><span class='heading'>" + attributes["NAME"] + "</span>";
        if(attributes["BUILDING_CODE"]){ html += "<br/><strong>Building:</strong> " + attributes["BUILDING_NAME"] + " (" + attributes["BUILDING_CODE"] + ")";}
        if(attributes["FLOOR"]){ html += "<br/><strong>Floor:</strong> " + getFloorLabel(attributes["FLOOR"]);}
        if(attributes["ROOM_NUMBER"]){ html += "<br/><strong>Room:</strong> " + attributes["BUILDING_CODE"] + " " + attributes["ROOM_NUMBER"];}
        if(attributes["CAMPUS_CODE"]){ html += "<br/><strong>Campus:</strong> " + attributes["CAMPUS_NAME"] + " (" + attributes["CAMPUS_CODE"] + ")";}
        if(attributes["CATEGORY_NAME"]){ html += "<br/><strong>Category:</strong> " + attributes["CATEGORY_NAME"];}
        if(attributes.DESCRIPTION)if(attributes["DESCRIPTION"] !== "Null"){ html += "<br/>" + attributes["DESCRIPTION"].replace("[rootdir]",FIUMAPS.Directories.Root);}
        html += "</div>";
        return html;
        
    },
    getPolygonHTMLWindows: function (attributes) {
        var html = "<div id='HTMLWindow'><span class='heading'>" + attributes["NAME"] + "</span>";
        if(attributes["CODE"]){ html += " <span class='heading'>(" + attributes["CODE"] + ")</span>"}
        if(attributes.DESCRIPTION)if(attributes["DESCRIPTION"] !== "Null"){ html += "<br/>" + attributes["DESCRIPTION"].replace("[rootdir]",FIUMAPS.Directories.Root);}
        html += "<br/><b>Location:</b> " + attributes["CAMPUS_NAME"];
        html += "</div>";
        return html;

    },    
    getServiceGridHTML: function(){

        var service_div = $('ServiceTabGrid').getElement('div');
        // recreate it for next time.
        var outerdiv = document.createElement('div');
        $(outerdiv).addClass('yui-skin-sam');
        var innerdiv = document.createElement('div');
        $(innerdiv).set({'id': 'yui_basic'});
        outerdiv.appendChild(innerdiv);
        $('ServiceTabGrid').appendChild(outerdiv);
        
        return service_div;
    },
    openPolygonPopup: function(attributes){

        FIUMAPS.HTMLPopUp.getServicesByBuildingID(attributes.BUILDING_ID); 
//          
        var html = FIUMAPS.HTMLPopUp.getPolygonHTMLWindows(attributes);
        
        FIUMAPS.HTMLPopUp.Tabs = [];     
        
        FIUMAPS.HTMLPopUp.Tabs.push(new GInfoWindowTab("Building", html));

        
    },
    getServicesByBuildingID_callBack: function (response,error){
        
        if(response){
            var resServices = response.features;

            if(resServices.length > 0){
                //loop through features  
                var services = [];
                var building;                
                //Loop through services results.
                for (var i=0; i<resServices.length; i++){
                    
                    services.push({NAME:"<a href='#' onclick='Javascript:FIUMAPS.HTMLPopUp.getServiceByID(" + resServices[i].attributes.OBJECTID + "); return false;'>" + resServices[i].attributes.NAME + "</a>", ROOM:resServices[i].attributes.ROOM_NUMBER});
                   // building = resServices[i].attributes.BUILDING_CODE;                    

                }
                var myColumnDefs = [ 
                    {key:"NAME", sortable:true, resizeable:true,width:248}, 
                    {key:"ROOM", sortable:true, resizeable:true,width:30}
                ];  
                                       
                var myDataSource = new YAHOO.util.DataSource(services); 
                myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
                myDataSource.responseSchema = {fields:["NAME","ROOM"]}; 

                var myDataTable = new YAHOO.widget.ScrollingDataTable("yui_basic", 
                        myColumnDefs, myDataSource, {height:"12em"}); 
                       
                FIUMAPS.HTMLPopUp.Tabs.push(new GInfoWindowTab("Services", FIUMAPS.HTMLPopUp.getServiceGridHTML()));                                        
            }
 
        }
        gmap.openInfoWindowTabsHtml(FIUMAPS.HTMLPopUp.Center, FIUMAPS.HTMLPopUp.Tabs/*,{onCloseFn: function() {FIUMAPS.Identify.clearResults(); }}*/);
    },
    getServicesByBuildingID: function (building_id){
        var url = "http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/0/query"
        var params = {  f:"json",
                        text:"ServicesByBuildingID",
                        where:"BUILDING_ID=" + building_id,
                        returnGeometry:false,
                        outFields:"OBJECTID,NAME,ROOM_NUMBER,BUILDING_CODE"
                     };
                     
        esri.arcgis.gmaps.JSONRequest(url, FIUMAPS.HTMLPopUp.getServicesByBuildingID_callBack,params);
    },
    getServiceByID: function (OBJECTID) {

        var url = "http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/0/query"
        var params = {  f:"json",
                        text:"ServiceByID",
                        where:"OBJECTID=" + OBJECTID,
                        returnGeometry:false,
                        outFields:"NAME,DESCRIPTION,FLOOR,ROOM_NUMBER,KEYWORDS,CATEGORY_ID,CATEGORY_NAME,BUILDING_CODE,BUILDING_NAME,CAMPUS_CODE,CAMPUS_NAME"
                     };
                     
        esri.arcgis.gmaps.JSONRequest(url, FIUMAPS.HTMLPopUp.getServiceByID_callBack,params);

    },
    getServiceByID_callBack: function (response,error) {
        if(response){
            var resServices = response.features;
            // use resServices[0] because there should always be only one result.
            if(gmap.getInfoWindow().getTabs().length < 3){
                FIUMAPS.HTMLPopUp.Tabs.push(new GInfoWindowTab(resServices[0].attributes.NAME.slice(0,5) + "...", FIUMAPS.HTMLPopUp.getMarkerHTMLWindows(resServices[0].attributes)));
            }else{
                FIUMAPS.HTMLPopUp.Tabs.pop();
                FIUMAPS.HTMLPopUp.Tabs.push(new GInfoWindowTab(resServices[0].attributes.NAME.slice(0,5) + "...", FIUMAPS.HTMLPopUp.getMarkerHTMLWindows(resServices[0].attributes)));
            }

            gmap.updateInfoWindow(FIUMAPS.HTMLPopUp.Tabs,FIUMAPS.HTMLPopUp.onServiceTabOpenUpdate);
                  
        }
    },
    onServiceTabOpenUpdate: function () {
        gmap.getInfoWindow().selectTab(gmap.getInfoWindow().getTabs().length - 1); 
    } 
};
